/*
 * Copyright (c) 2015 www.smarthome.com All rights reserved. 本软件源代码版权归----所有,未经许可不得任意复制与传播.
 */
package com.smarthome.base.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.dao.ShUserDao;
import com.smarthome.base.dao.base.BaseDao;
import com.smarthome.base.service.ShUserService;
import com.smarthome.base.service.base.BaseServiceImpl;
import com.smarthome.domain.bean.ShUser;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * ShUserService 实现类
 * @author KongXiangyu
 * @since 2015-09-07
 */
@Service("shUserService")
public class ShUserServiceImpl extends BaseServiceImpl<ShUser, Long> implements ShUserService {

    @Resource
    private ShUserDao shUserDao;

    public BaseDao<ShUser, Long> getDao() {
        return shUserDao;
    }
    @Override
    public DataGrid queryShUsers(ShUser user, PageHelper page) {
        DataGrid dataGrid = new DataGrid();
        ShUser queryUser = new ShUser();
        if (!StringUtils.isBlank(page.getSort()))
            queryUser.setOrderField(page.getSort());
        if (!StringUtils.isBlank(page.getOrder()))
            queryUser.setOrderFieldType(page.getOrder());
        if (!StringUtils.isBlank(user.getLoginName()))
            queryUser.addQueryData("loginName", user.getLoginName());
        if (null != user.getStartGmtCreate())
            queryUser.addQueryData("startGmtCreate", user.getStartGmtCreate());
        if (null != user.getEndGmtCreate())
            queryUser.addQueryData("endGmtCreate", user.getEndGmtCreate());
        if (null != user.getStartGmtModify())
            queryUser.addQueryData("startGmtModify", user.getStartGmtModify());
        if (null != user.getEndGmtModify())
            queryUser.addQueryData("endGmtModify", user.getEndGmtModify());
        if(null != user.getResidenceId()) queryUser.setResidenceId(user.getResidenceId());
        long totalCount = shUserDao.queryShUsersCount(queryUser);
        dataGrid.setTotal(totalCount);
        int startIndex = (page.getPage() - 1) * page.getRows();
        int endIndex = page.getPage() * page.getRows();
        if (totalCount > 0 && totalCount > startIndex) {
            queryUser.setStartIndex(startIndex);
            queryUser.setEndIndex(endIndex);
            List<ShUser> shUsers = shUserDao.queryShUsers(queryUser);
            dataGrid.setRows(shUsers);
        }
        return dataGrid;
    }
}